let { addGood, searchGood, getDetailGood,updateDetail,deleteGood } = require("../dao/goodsDao");
let { moveFiles, deleteFiles } = require("../utils/handFiles");
module.exports.addGood = async function (data) {
    let res = await addGood(data);

    if (res._id) {
        moveFiles({
            fromPath: "./public/temp",
            toPath: "./public/images",
            filename: data.img,
        });
        deleteFiles(`./public/temp/${data.img}`);
        return {
            code: 1,
            msg: "操作成功!",
        };
    }
    // console.log(res)
    return {
        code: 0,
        msg: "操作失败",
    };
};

module.exports.searchGood = async function (data) {
    let res = await searchGood(data);
    // console.log(res)
    if (res.result.length) {
        return {
            code: 1,
            data: res,
            msg: "操作成功!",
        };
    }
    return {
        code: 1,
        data: res,
        msg: "操作失败!",
    };
};

module.exports.getDetailGood = async function (data) {
    let res = await getDetailGood(data);
    if (res.length) {
        return {
            code: 1,
            data: res[0],
            msg: "操作成功",
        };
    }
    return {
        code: 0,
        msg: "操作失败",
    };
};

module.exports.updateDetail=async(data)=>{
    let res= await updateDetail(data)
    console.log(res)
    if(res.modifiedCount===1){
        moveFiles({
            fromPath: "./public/temp",
            toPath: "./public/images",
            filename: data.img,
        });
        deleteFiles(`./public/temp/${data.img}`);
        return {
            code:1,
            msg:"编辑成功!"
        }
    }
    return {
        code:0,
        msg:"编辑失败!"
    }
}
//更新状态
module.exports.updateStatus=async(data)=>{
    let res= await updateDetail(data)
    if(res.modifiedCount===1){
        return {
            code:1,
            msg:"操作成功!"
        }
    }
    return {
        code:0,
        msg:"操作失败!"
    }
}
module.exports.deleteGood=async(data)=>{
    let res= await deleteGood(data)
    console.log(res)
    if(res.deletedCount===1){
        return {
            code:1,
            msg:"删除成功!"
        }
    }
    return {
        code:0,
        msg:"删除失败!"
    }

}